package com.bhs.service;

import com.bhs.mapper.OrderMapper;
import com.bhs.mapper.RemoteUserService;
import com.bhs.pojo.Order;
import com.bhs.pojo.OrderItem;
import com.bhs.pojo.OrderVO;
import com.bhs.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class OrderServiceImpl implements OrderService{
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RemoteUserService remoteUserService;

    @Override
    public List<Order> getOrderDataById(String username) {
        User user = remoteUserService.selectUserByUsername(username);
        Long userId = user.getId();
        List<Order> orderList = new ArrayList<>();
        //通过用户id查询订单
        List<Order> list = orderMapper.getOrderListById(userId);
        //遍历订单，获取订单id,通过订单id查询商品存入集合
        for (Order o :  list) {
            String id = o.getOrderId();
            //基于订单id查询商品
            List<OrderItem> item = orderMapper.getOrderItemListByOrderId(id);
            //将商品放入订单内
            o.setChildren(item);
            //将每个订单放入订单集合内
            orderList.add(o);
        }
        return orderList;
    }
}
